package com.iterator.controller.web.goodsManager;

import com.iterator.bean.web.pojo.tcy.Goods;
import com.iterator.bean.web.stone.BaseRespVo;
import com.iterator.bean.web.tcy.CatAndBrandVO;
import com.iterator.bean.web.tcy.GoodsAddAndUpdateBO;
import com.iterator.bean.web.tcy.GoodsDetailVO;
import com.iterator.bean.web.tcy.GoodsListVO;
import com.iterator.service.web.goodsManager.GoodsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.*;

/**
 * @author tean_cy
 * @date 2021/1/9 22:21
 */

@RestController
@RequestMapping("admin/goods")
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    @RequestMapping("list")
    @RequiresPermissions("admin:goods:list")
    public BaseRespVo list(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit,
                           @Nullable@RequestParam("goodsSn") String goodsSn, @Nullable@RequestParam("name")String name,
                           @RequestParam("sort")String sort, @RequestParam("order") String order){

        //TODO 需要对请求参数进行判断，非空的参数如果为空则需要抛异常

        GoodsListVO goodsListVO = goodsService.list(page,limit,goodsSn,name,sort,order);
        return BaseRespVo.ok(goodsListVO);

    }

    @RequestMapping("catAndBrand")
    public BaseRespVo catAndBrand(){
        CatAndBrandVO catAndBrandVO = goodsService.catAndBrand();
        return BaseRespVo.ok(catAndBrandVO);
    }

    @RequestMapping("create")
    @RequiresPermissions("admin:goods:create")
    public BaseRespVo create(@RequestBody GoodsAddAndUpdateBO createBO){
        int code = goodsService.create(createBO);
        if (code == 1){
            return BaseRespVo.ok();
        }
        if (code == 205){
            return BaseRespVo.fail("商品编号已存在");
        }
        return BaseRespVo.fail("添加失败");
    }

    @RequestMapping("update")
    @RequiresPermissions("admin:goods:update")
    public BaseRespVo update(@RequestBody GoodsAddAndUpdateBO updateBO){
        int code = goodsService.update(updateBO);
        if (code != 0){
        return BaseRespVo.ok();
        }
        return BaseRespVo.fail("更新失败");
    }


    @RequestMapping("detail")
    @RequiresPermissions("admin:goods:read")
    public BaseRespVo detail(@RequestParam("id") Integer id){
        GoodsDetailVO goodsDetailVO = goodsService.detail(id);
        return BaseRespVo.ok(goodsDetailVO);
    }

    @RequestMapping("delete")
    @RequiresPermissions("admin:goods:delete")
    public BaseRespVo delete(@RequestBody Goods goods){
        int code = goodsService.delete(goods);
        if (code != 0){
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail("删除失败");
    }

}
